#include <Arduino.h>

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#include <log.h>

const char *ssid     = "CMCC-bygya818";
const char *password = "bygya818";

const long utcOffsetInSeconds = 3600;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 8 * utcOffsetInSeconds);
extern NTPClient g_time_client;

// put function declarations here:

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(74880);

  // connect to WiFi
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  timeClient.begin();
  g_time_client.begin();
}

void loop()
{
  // put your main code here, to run repeatedly:
  timeClient.update();

  unsigned long epco_time = timeClient.getEpochTime();
  Serial.printf("loop...epco:%lu\n", epco_time);
  tm *date_time = gmtime((time_t *)&epco_time);
  Serial.printf("loop...%04d-%02d-%02d ", date_time->tm_year + 1900, date_time->tm_mon + 1, date_time->tm_mday);

  Serial.print(daysOfTheWeek[timeClient.getDay()]);
  Serial.print(" ");
  Serial.println(timeClient.getFormattedTime());

  LOG(1, "Show NTP time");

  delay(10000);
}

// put function definitions here: